package com.kchouse.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.SharedPreferencesUtil;
import com.kchouse.common.ToastUtils;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class AlterpsActivity extends BaseActivity implements View.OnClickListener {

    private AlterpsActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private EditText et_oldpassword;
    private EditText et_password;
    private EditText et_password2;
    private Button bt_item;
    private String oldps;
    private String newps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alterps);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("修改密码");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        et_oldpassword = (EditText) findViewById(R.id.et_oldpassword);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password2 = (EditText) findViewById(R.id.et_password2);
        bt_item = (Button) findViewById(R.id.bt_item);
        bt_item.setOnClickListener(this);
    }

    private void intiData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_item:
                if (TextUtils.isEmpty(et_oldpassword.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("原密码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(et_password.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("新密码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(et_password2.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("确认密码不能为空");
                    return;
                }
                oldps = et_oldpassword.getText().toString();
                newps = et_password.getText().toString();
                if (!newps.equals(et_password2.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("密码不一致");
                    return;
                } else {
//                    ToastUtils.getInstanc(mContent).showToast("原密码："+oldps+"  新密码："+newps);
                    PostAlter();
                }

                break;
        }
    }

    /**
     * 提交修改密码
     */
    private void PostAlter() {
        String url = API.PostAlter;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        JSONObject ob = new JSONObject();
        ob.put("userId", MainActivity.userid);
        ob.put("oldPwd", oldps);
        ob.put("newPwd", newps);
//        params.addFormDataPart("file", file);//上传单个文件
//        params.addFormDataPart("files", files);//上传多个文件
//        params.addHeader("token", token);//添加header信息
        params.applicationJson(ob);

        HttpRequest.put(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    ToastUtils.getInstanc(mContent).showToast("修改成功");
                    finish();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }
}
